/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef SCA3000Test_H_
#define SCA3000Test_H_

#include "TestCase.h"
#include "Coms/SPI/SPIMaster.h"
#include "Sensors/SCA3000.h"
#include <stdio.h>
#include <math.h>

using Coms::SPI::SPIMaster;
using Coms::SPI::SPIHardware;

#define READ_MARGIN 10

class SCA3000Test : public TestCase<SCA3000Test>
{
public:

	SCA3000Test(const prog_mem * testMethodName_P, TestCase<SCA3000Test>::TestMethod testMethod) :
	TestCase<SCA3000Test>(testMethodName_P, testMethod)
	{

//		DDRB = (1<<PINB0) | (1<<PINB1) | (1<<PINB2); // set SCK and MOSI as output

		spiHardware = SPIHardware::getSPIHardware(0);
		spiHardware->setPrescaler(Coms::SPI::Prescalers::DIV_128, false);
		spiHardware->setSPIPinsDirections(OUTPUT, OUTPUT, OUTPUT, OUTPUT);
		spiHardware->setMaster();
		spiHardware->transmitMSBFirst();
		spiHardware->enable();


		spiMaster = new SPIMaster(spiHardware, &PORTC, &DDRC, PINC0);
		sca3000x = new SCA3000(spiMaster, 0x05);
		sca3000y = new SCA3000(spiMaster, 0x07);
		sca3000z = new SCA3000(spiMaster, 0x09);

	}

	void setUp()
	{
		enableInterrupts();
	}

	void tearDown()
	{
		disableInterrupts();
		spiHardware->disable();
	}

	bool testRead()
	{

		bool result = AVRUNIT_ASSERT_IN_RANGE_LONG(3*(int)sca3000y->read().serialize().buffer/32, 0, 50);
		result &= AVRUNIT_ASSERT_IN_RANGE_LONG(3*(int)sca3000y->read().serialize().buffer/32, 0, 50);
		result &= AVRUNIT_ASSERT_IN_RANGE_LONG(3*(int)sca3000z->read().serialize().buffer/32, -1000, 50);

		return result;

	}

	SPIMaster * spiMaster;
	SPIHardware * spiHardware;
	SCA3000 * sca3000x;
	SCA3000 * sca3000y;
	SCA3000 * sca3000z;

};
#endif /* SCA3000Test_H_ */
